package cn.cheqi.recovery.background.controller;


import cn.cheqi.recovery.background.dto.DataGridView;
import cn.cheqi.recovery.background.dto.ResultDTO;
import cn.cheqi.recovery.background.entity.Order;
import cn.cheqi.recovery.background.mapper.OrderMapper;
import cn.cheqi.recovery.background.service.OrderService;
import cn.cheqi.recovery.background.vo.OrderVO;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  订单前端控制器
 * </p>
 *
 * @author Robin
 * @since 2020-02-23
 */
@RestController
@RequestMapping("/system/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 加载所有订单
     * @param orderVO
     * @return
     */
    @RequestMapping("/loadAllOrders")
    public DataGridView loadAllOrders(OrderVO orderVO){
        IPage<Order> page = new Page<>(orderVO.getPage(),orderVO.getLimit());
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        //获取四个模糊查询的条件
        //订单编号
        Long orderId = orderVO.getOrderId();
        //订单流水号
        String orderNum = orderVO.getOrderNum();
        //去掉查询条件里面的空格
        if (orderId != null){
            wrapper.like("order_id",orderId);
        }
        if (!StringUtils.isBlank(orderNum)){
            String newNum = orderNum.replace(" ", "");
            wrapper.like(!StringUtils.isBlank(newNum),"order_num",newNum);
        }
        //查询时间范围
        wrapper.ge(orderVO.getStartDate() != null,"appointment_time",orderVO.getStartDate());
        wrapper.le(orderVO.getEndDate() != null,"appointment_time",orderVO.getEndDate());
        wrapper.orderByDesc("order_id");
        orderService.page(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 通过订单ID列出订单详情,包含该订单所属的业主信息和回收员工信息
     * @param orderVO
     * @return
     */
    @RequestMapping("/detailOrderById")
    public OrderVO detailOrderByOrderId(OrderVO orderVO){
        OrderVO orderDetail = orderMapper.findOrderDetailsByOrderId(orderVO.getOrderId());
        return orderDetail;
    }

    /**
     * 添加订单
     * @param orderVO
     * @return
     */
    @PostMapping("/addOrder")
    @Transactional
    public ResultDTO addOrder(OrderVO orderVO){
        try {
            //生成订单流水号
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String beforeNum = sdf.format(date);
            //日期加上随机生成的字符串生成订单号
            String orderNum = beforeNum + IdUtil.simpleUUID().toUpperCase();
            orderVO.setOrderNum(orderNum);
            orderService.save(orderVO);
            return new ResultDTO(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO(-1,"添加失败");
        }
    }

    /**
     * 通过订单ID删除订单
     * @param orderVO
     * @return
     */
    @PostMapping("/deleteOrderById")
    @Transactional
    public ResultDTO deleteOrderById(OrderVO orderVO){
        try {
            orderService.removeById(orderVO.getOrderId());
            return new ResultDTO(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO(-1,"删除失败");
        }
    }

    /**
     * 通过订单ID更新订单
     * @param orderVO
     * @return
     */
    @PostMapping("/editOrderById")
    @Transactional
    public ResultDTO editOrderById(OrderVO orderVO){
        try {
            orderService.updateById(orderVO);
            return new ResultDTO(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO(-1,"修改失败");
        }
    }
}

